{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['age', 'pclass=1st', 'pclass=2nd', 'pclass=3rd', 'sex=female', 'sex=male']\n",
      "  (0, 0)\t30.0\n",
      "  (0, 2)\t1.0\n",
      "  (0, 5)\t1.0\n",
      "  (1, 0)\t62.0\n",
      "  (1, 1)\t1.0\n",
      "  (1, 5)\t1.0\n",
      "  (2, 0)\t31.19418104265403\n",
      "  (2, 3)\t1.0\n",
      "  (2, 4)\t1.0\n",
      "  (3, 0)\t31.19418104265403\n",
      "  (3, 1)\t1.0\n",
      "  (3, 4)\t1.0\n",
      "  (4, 0)\t64.0\n",
      "  (4, 2)\t1.0\n",
      "  (4, 5)\t1.0\n",
      "  (5, 0)\t31.19418104265403\n",
      "  (5, 1)\t1.0\n",
      "  (5, 4)\t1.0\n",
      "  (6, 0)\t24.0\n",
      "  (6, 3)\t1.0\n",
      "  (6, 4)\t1.0\n",
      "  (7, 0)\t31.19418104265403\n",
      "  (7, 3)\t1.0\n",
      "  (7, 5)\t1.0\n",
      "  (8, 0)\t31.19418104265403\n",
      "  :\t:\n",
      "  (975, 4)\t1.0\n",
      "  (976, 0)\t18.0\n",
      "  (976, 2)\t1.0\n",
      "  (976, 4)\t1.0\n",
      "  (977, 0)\t31.19418104265403\n",
      "  (977, 3)\t1.0\n",
      "  (977, 4)\t1.0\n",
      "  (978, 0)\t31.19418104265403\n",
      "  (978, 2)\t1.0\n",
      "  (978, 5)\t1.0\n",
      "  (979, 0)\t31.19418104265403\n",
      "  (979, 2)\t1.0\n",
      "  (979, 5)\t1.0\n",
      "  (980, 0)\t28.0\n",
      "  (980, 3)\t1.0\n",
      "  (980, 5)\t1.0\n",
      "  (981, 0)\t34.0\n",
      "  (981, 2)\t1.0\n",
      "  (981, 5)\t1.0\n",
      "  (982, 0)\t46.0\n",
      "  (982, 1)\t1.0\n",
      "  (982, 5)\t1.0\n",
      "  (983, 0)\t31.19418104265403\n",
      "  (983, 3)\t1.0\n",
      "  (983, 5)\t1.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ThinkPad\\AppData\\Local\\Temp\\ipykernel_12296\\773770983.py:6: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  x['age'].fillna(x['age'].mean(), inplace=True)\n",
      "C:\\Program Files\\Python39\\lib\\site-packages\\sklearn\\utils\\deprecation.py:87: FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated in 1.0 and will be removed in 1.2. Please use get_feature_names_out instead.\n",
      "  warnings.warn(msg, category=FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "titan = pd.read_csv('./data/titanic.txt')\n",
    "\n",
    "x = titan[['pclass', 'age', 'sex']]\n",
    "y = titan['survived']\n",
    "\n",
    "x['age'].fillna(x['age'].mean(), inplace=True)\n",
    "\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=4)\n",
    "dict = DictVectorizer()\n",
    "# 这一步是对字典进行特征抽取,to_dict可以把df变为字典，records代表列名变为键\n",
    "x_train = dict.fit_transform(x_train.to_dict(orient=\"records\"))\n",
    "print(dict.get_feature_names())\n",
    "x_test = dict.transform(x_test.to_dict(orient=\"records\"))\n",
    "print(x_train)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率： 0.8237082066869301\n",
      "查看选择的参数模型： {'max_depth': 3, 'n_estimators': 50}\n",
      "选择最好的模型是： RandomForestClassifier(max_depth=3, n_estimators=50, n_jobs=-1)\n",
      "每个超参数每次交叉验证的结果： {'mean_fit_time': array([0.12451235, 0.13523173, 0.11350846, 0.19303536, 0.20324651,\n",
      "       0.20704937, 0.13996967, 0.13861068, 0.24248791, 0.32334733,\n",
      "       0.09751534, 0.20440674]), 'std_fit_time': array([0.01045455, 0.00876158, 0.01954253, 0.08817083, 0.08037678,\n",
      "       0.11728618, 0.05077176, 0.02123116, 0.0051089 , 0.0097904 ,\n",
      "       0.006928  , 0.08995993]), 'mean_score_time': array([0.02593358, 0.03499921, 0.03296407, 0.04281044, 0.0636154 ,\n",
      "       0.03160055, 0.03913713, 0.03940479, 0.06322273, 0.07979472,\n",
      "       0.02894457, 0.04583995]), 'std_score_time': array([0.00367893, 0.00256201, 0.00279824, 0.01504181, 0.02883627,\n",
      "       0.00211289, 0.01984076, 0.01106965, 0.00259879, 0.01516783,\n",
      "       0.01060528, 0.01087368]), 'param_max_depth': masked_array(data=[2, 2, 3, 3, 5, 5, 8, 8, 15, 15, 25, 25],\n",
      "             mask=[False, False, False, False, False, False, False, False,\n",
      "                   False, False, False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'param_n_estimators': masked_array(data=[50, 80, 50, 80, 50, 80, 50, 80, 50, 80, 50, 80],\n",
      "             mask=[False, False, False, False, False, False, False, False,\n",
      "                   False, False, False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'params': [{'max_depth': 2, 'n_estimators': 50}, {'max_depth': 2, 'n_estimators': 80}, {'max_depth': 3, 'n_estimators': 50}, {'max_depth': 3, 'n_estimators': 80}, {'max_depth': 5, 'n_estimators': 50}, {'max_depth': 5, 'n_estimators': 80}, {'max_depth': 8, 'n_estimators': 50}, {'max_depth': 8, 'n_estimators': 80}, {'max_depth': 15, 'n_estimators': 50}, {'max_depth': 15, 'n_estimators': 80}, {'max_depth': 25, 'n_estimators': 50}, {'max_depth': 25, 'n_estimators': 80}], 'split0_test_score': array([0.73780488, 0.73780488, 0.80182927, 0.80487805, 0.81097561,\n",
      "       0.81097561, 0.82012195, 0.82012195, 0.81402439, 0.81402439,\n",
      "       0.81097561, 0.82012195]), 'split1_test_score': array([0.82317073, 0.82317073, 0.83231707, 0.82317073, 0.82012195,\n",
      "       0.81707317, 0.82012195, 0.81707317, 0.81097561, 0.81707317,\n",
      "       0.81097561, 0.81097561]), 'split2_test_score': array([0.81402439, 0.81707317, 0.82926829, 0.82926829, 0.82621951,\n",
      "       0.82621951, 0.80792683, 0.81707317, 0.78963415, 0.79268293,\n",
      "       0.79573171, 0.79268293]), 'mean_test_score': array([0.79166667, 0.79268293, 0.82113821, 0.81910569, 0.81910569,\n",
      "       0.81808943, 0.81605691, 0.81808943, 0.80487805, 0.80792683,\n",
      "       0.80589431, 0.80792683]), 'std_test_score': array([0.03826864, 0.0388844 , 0.0137101 , 0.01036386, 0.00626465,\n",
      "       0.00626465, 0.00574884, 0.00143721, 0.01085069, 0.01085069,\n",
      "       0.00718604, 0.01140749]), 'rank_test_score': array([12, 11,  1,  2,  2,  4,  6,  4, 10,  7,  9,  7])}\n"
     ]
    }
   ],
   "source": [
    "# 随机森林进行预测 （超参数调优），n_jobs充分利用多核的一个参数\n",
    "rf = RandomForestClassifier(n_jobs=-1)\n",
    "# 120, 200, 300, 500, 800, 1200,n_estimators森林中决策树的数目，也就是分类器的数目\n",
    "# max_samples  是最大样本数\n",
    "#bagging类型\n",
    "param = {\"n_estimators\": [50,80], \"max_depth\": [2, 3, 5, 8, 15, 25]}\n",
    "# 网格搜索与交叉验证\n",
    "gc = GridSearchCV(rf, param_grid=param, cv=3)\n",
    "\n",
    "gc.fit(x_train, y_train)\n",
    "\n",
    "print(\"准确率：\", gc.score(x_test, y_test))\n",
    "\n",
    "print(\"查看选择的参数模型：\", gc.best_params_)\n",
    "\n",
    "print(\"选择最好的模型是：\", gc.best_estimator_)\n",
    "\n",
    "print(\"每个超参数每次交叉验证的结果：\", gc.cv_results_)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}